package com.lsx143.wordcount.day5;

import com.lsx143.wordcount.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class Flink_TumblingWindowFunction_Agg {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env
                .socketTextStream("hadoop162", 9999)
                .flatMap(new FlatMapFunction<String, WaterSensor>() {
                    @Override
                    public void flatMap(String line, Collector<WaterSensor> out) throws Exception {
                        String[] datas = line.split(",");
                        out.collect(new WaterSensor(datas[0], Long.valueOf(datas[1]), Integer.valueOf(datas[2])));
                    }
                })
                .keyBy(WaterSensor::getId)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .aggregate(new AggregateFunction<WaterSensor, MyAVG, Double>() {
                    @Override
                    public MyAVG createAccumulator() {
                        System.out.println("Flink_TumblingWindowFunction_Agg.createAccumulator");
                        return new MyAVG();
                    }

                    @Override
                    public MyAVG add(WaterSensor waterSensor, MyAVG accumulator) {
                        MyAVG.sum += waterSensor.getVc();
                        MyAVG.count++;
                        System.out.println("Flink_TumblingWindowFunction_Agg.add");
                        return accumulator;
                    }

                    @Override
                    public Double getResult(MyAVG accumulator) {
                        System.out.println("Flink_TumblingWindowFunction_Agg.getResult");
                        return MyAVG.getAvg();
                    }

                    @Override
                    public MyAVG merge(MyAVG a, MyAVG b) {
                        System.out.println("Flink_TumblingWindowFunction_Agg.merge");
                        return null;
                    }
                })
                .print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class MyAVG {
        public static Integer sum = 0;
        public static Integer count = 0;

        public static Double getAvg() {
            return sum * 1.0 / count;
        }
    }
}